import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/comment/comment_model.dart';
import 'package:xvideos/hj_model/comment/reply_model.dart';
import 'package:xvideos/hj_page/comment/cell/comment_item.dart';
import 'package:xvideos/hj_page/comment/comment_mixin.dart';
import 'package:xvideos/hj_page/comment/widget/comment_foot_view.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

import '../home_business/widget/quick_search_view.dart';

class CommentController extends GetxController with CommentMixin {
  final String objId;

  final String commentType;

  @override
  final Function(int)? totalCallback;

  CommentController(
    this.objId, {
    this.commentType = 'video',
    this.totalCallback,
  });

  bool isLoading = true;

  @override
  void onInit() {
    super.onInit();
    id = objId;
    objType = commentType;
  }

  @override
  void onReady() {
    super.onReady();
    isLoading = false;
    fetchComments(objId);
  }

  onCommentOrReplyAction({CommentModel? comment, ReplyModel? reply, bool? isSend}) async {
    readyReplyComment(comment, replyModel: reply, isSend: isSend);
  }
}

class CommentView extends StatefulWidget {
  final bool showInput;
  final String objId;
  final String objType; // video,  cartoon
  final Function(int)? totalCallback;

  const CommentView(
    this.objId, {
    this.showInput = true,
    this.objType = 'video',
    this.totalCallback,
  });

  @override
  State<CommentView> createState() => _CommentViewState();
}

class _CommentViewState extends State<CommentView> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: CommentController(
        widget.objId,
        commentType: widget.objType,
        totalCallback: widget.totalCallback,
      ),
      tag: "${widget.objId}",
      builder: (controller) => LayoutBuilder(builder: (_, cons) {
        if (cons.maxHeight < 58) return SizedBox.shrink();
        return Column(
          children: [
            Expanded(
              child: pullYsRefresh(
                onInit: (_) => controller.refreshController = _,
                onLoading: (_) => controller.fetchComments(widget.objId),
                onRefresh: (_) => controller.fetchComments(widget.objId, isRefresh: true),
                child: () {
                  if (controller.isLoading) return LoadingCenterWidget();
                  if (controller.comments.isEmpty) return CErrorWidget();
                  return CustomScrollView(
                    slivers: [
                      if (controller.quickSearchList.isNotEmpty == true)
                        SliverToBoxAdapter(
                          child: Container(
                            padding: EdgeInsets.only(bottom: 12),
                            margin: EdgeInsets.fromLTRB(12, 0, 12, 12),
                            decoration: BoxDecoration(border: Border(bottom: BorderSide(color: Color(0x08ffffff)))),
                            child: QuickSearchView(controller.quickSearchList),
                          ),
                        ),
                      SliverList.builder(
                        itemCount: controller.comments.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Container(
                            padding: EdgeInsets.symmetric(horizontal: 16),
                            child: CommentItem(
                              key: ValueKey(controller.comments[index].id),
                              controller.comments[index],
                              objId: widget.objId,
                              replyHandler: (p0, {reply}) {
                                controller.onCommentOrReplyAction(comment: p0, reply: reply);
                              },
                            ),
                          );
                        },
                      ),
                    ],
                  );
                }(),
              ),
            ),
            if (widget.showInput)
              Obx(
                () => CommentFootView(
                  isSendStatus: controller.isSendingMsg.value,
                  begainCommentHandler: (value) => controller.onCommentOrReplyAction(isSend: value),
                  textEditController: controller.commentController,
                  imagePath: controller.selectedImagePath.value,
                  imgCallback: (value) {
                    controller.selectedImagePath.value = value;
                  },
                ),
              ),
          ],
        );
      }),
      global: false,
    );
  }
}
